package com.ruoyi.business.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.business.service.impl.HikVisionServiceImpl;
import com.ruoyi.business.vo.MatchList;
import com.ruoyi.business.vo.SearchResult;
import com.ruoyi.business.vo.WxDeviceConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.hash.BeanUtilsHashMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.WxFaceDevice;
import com.ruoyi.business.service.IWxFaceDeviceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 人脸设别Controller
 * 
 * @author asher
 * @date 2021-11-27
 */
@RestController
@RequestMapping("/business/face_device")
public class WxFaceDeviceController extends BaseController
{
    @Autowired
    private IWxFaceDeviceService wxFaceDeviceService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    @Qualifier("HikVisionService")
    private HikVisionServiceImpl hikVisionService;

    /**
     * 查询人脸设别列表
     */
    @PreAuthorize("@ss.hasPermi('business:face_device:list')")
    @GetMapping("/list")
    public TableDataInfo list(WxFaceDevice wxFaceDevice) throws IllegalAccessException {
        startPage();
        SearchResult deviceList = new SearchResult();;
        try {
            deviceList = hikVisionService.getDeviceList();
        } catch (Exception e) {
            deviceList.setMatchList(new ArrayList<>());
            e.printStackTrace();
        }
        List<WxFaceDevice> list = wxFaceDeviceService.selectWxFaceDeviceList(wxFaceDevice);
        Iterator<WxFaceDevice> iterator = list.iterator();

        List<MatchList> matchList = deviceList.getMatchList();
        while (iterator.hasNext()) {
            WxFaceDevice next = iterator.next();
            Boolean online = false;
            if (WxDeviceConstants.HIK_DEVICE.equals(next.getDeviceType())) {
                for (MatchList item : matchList) {
                    if (next.getHikDeviceIndex().equals(item.getDevice().getDevIndex()) && item.getDevice().getDevStatus().equals("online")) {
                        online =  true;
                    }
                }
            } else {
                online = redisCache.getCacheObject("SN_ONLINE_" + next.getSn());
            }
            if (null != online) {
                next.setOnline(online);
            }
            if (null != online && !WxDeviceConstants.HIK_DEVICE.equals(next.getDeviceType())) {
                Object configObject = redisCache.getCacheObject("SN_CONFIG_" + next.getSn());
                Map<String, Object> objectToMap = BeanUtils.getObjectToMap(configObject);
                Integer personCount = (Integer)objectToMap.get("personCount");
                Integer faceCount = (Integer)objectToMap.get("faceCount");
                next.setFaceCount(Long.valueOf(faceCount));
                next.setPersonCount(Long.valueOf(personCount));
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出人脸设别列表
     */
    @PreAuthorize("@ss.hasPermi('business:face_device:export')")
    @Log(title = "人脸设别", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WxFaceDevice wxFaceDevice)
    {
        List<WxFaceDevice> list = wxFaceDeviceService.selectWxFaceDeviceList(wxFaceDevice);
        ExcelUtil<WxFaceDevice> util = new ExcelUtil<WxFaceDevice>(WxFaceDevice.class);
        return util.exportExcel(list, "人脸设别数据");
    }

    /**
     * 获取人脸设别详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:face_device:query')")
    @GetMapping(value = "/{faceDeviceId}")
    public AjaxResult getInfo(@PathVariable("faceDeviceId") Long faceDeviceId)
    {
        return AjaxResult.success(wxFaceDeviceService.selectWxFaceDeviceByFaceDeviceId(faceDeviceId));
    }

    /**
     * 新增人脸设别
     */
    @PreAuthorize("@ss.hasPermi('business:face_device:add')")
    @Log(title = "人脸设别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WxFaceDevice wxFaceDevice)
    {
        int i = wxFaceDeviceService.insertWxFaceDevice(wxFaceDevice);
        if (i != 1) {
            return AjaxResult.error("添加失败,请重试");
        }
        return toAjax(i);
    }

    /**
     * 修改人脸设别
     */
    @PreAuthorize("@ss.hasPermi('business:face_device:edit')")
    @Log(title = "人脸设别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WxFaceDevice wxFaceDevice)
    {
        return toAjax(wxFaceDeviceService.updateWxFaceDevice(wxFaceDevice));
    }

    /**
     * 删除人脸设别
     */
    @PreAuthorize("@ss.hasPermi('business:face_device:remove')")
    @Log(title = "人脸设别", businessType = BusinessType.DELETE)
	@DeleteMapping("/{faceDeviceIds}")
    public AjaxResult remove(@PathVariable Long[] faceDeviceIds)
    {
        return toAjax(wxFaceDeviceService.deleteWxFaceDeviceByFaceDeviceIds(faceDeviceIds));
    }
}
